package com.ysd.lis.service.eq.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.EqSupplier;
import com.ysd.lis.entity.eq.EqSupplier;
import com.ysd.lis.entity.lab.LabSupplier;
import com.ysd.lis.entity.lab.LabSupplierAppraise;
import com.ysd.lis.mapper.eq.EqSupplierMapper;
import com.ysd.lis.request.EqSupplierDto;
import com.ysd.lis.request.lab.SupplierPageParam;
import com.ysd.lis.service.eq.EqSupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备管理-供应商 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-07-30
 */
@Service
public class EqSupplierServiceImpl extends ServiceImpl<EqSupplierMapper, EqSupplier> implements EqSupplierService {

    @Autowired
    private EqSupplierMapper eqSupplierMapper;
    @Override
    public Result findSupplierPageList(SupplierPageParam pageParam) {
        Page<EqSupplier> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<EqSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqSupplier::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqSupplier::getSupplierName, pageParam.getSeachValue())
                    .or().like(EqSupplier::getSupplierCode, pageParam.getSeachValue())
                    .or().like(EqSupplier::getInputCode,pageParam.getSeachValue()));
        }
        if (StringUtils.isNotBlank(pageParam.getSupplierType())){
            lambdaQueryWrapper.eq(EqSupplier::getSupplierType,pageParam.getSupplierType());
        }
        if(ToolsUtils.isNotEmpty(pageParam.getIsStop())){
            lambdaQueryWrapper.eq(EqSupplier::getIsStop,pageParam.getIsStop());
        }
        lambdaQueryWrapper.orderByAsc(EqSupplier::getSeq);
        Page<EqSupplier> supplierPage = eqSupplierMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addSupplier(EqSupplier supplier) {
        int insert = eqSupplierMapper.insert(supplier);
        if(insert>0){
            return Result.succ(1,"添加成功",supplier);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editSupplier(EqSupplier supplier) {
        int updateById = eqSupplierMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteSupplier(EqSupplier supplier) {
        int deleteById = eqSupplierMapper.deleteById(supplier.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteSupplierList(EqSupplierDto dto) {
        List<EqSupplier> supplierList = dto.getSupplierList();
        List<String> ids = supplierList.stream().map(EqSupplier::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
